clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\KTHDP"

*Merge predictions
use PredEUCARAKTHDP, clear
merge 1:1 subj using propcorrpredTWTTh2, nogen
merge 1:1 subj using PredEUCRRAKTHDP, nogen
merge 1:1 subj using PredEURPMCRRAKTHDP, nogen
merge 1:1 subj using PredEURPMCRRAKTHDP, nogen

sum corrpredeuCARA* corrpredeuCRRA* corrpredeuRPMCARA* corrpredeuRPMCRRA*
sum corrpredTWT*
*Average across the out-of-sample predictions
gen corrpredTWT = (corrpredTWT1+ corrpredTWT2+corrpredTWT3+corrpredTWT4+corrpredTWT5)/5
gen corrpredCARA = (corrpredeuCARA1+ corrpredeuCARA2+corrpredeuCARA3+corrpredeuCARA4+corrpredeuCARA5)/5
gen corrpredCRRA = (corrpredeuCRRA1+ corrpredeuCRRA2+ corrpredeuCRRA3+corrpredeuCRRA4+corrpredeuCRRA5)/5
gen corrpredRPMCRRA = (corrpredeuRPMCRRA1+ corrpredeuRPMCRRA2+corrpredeuRPMCRRA3+corrpredeuRPMCRRA4+corrpredeuRPMCRRA5)/5
gen corrpredRPMCARA = (corrpredeuRPMCARA1+ corrpredeuRPMCARA2+corrpredeuRPMCARA3+corrpredeuRPMCARA4+corrpredeuRPMCARA5)/5

*Tests comparing predictive performances
sum corrpredTWT corrpredCARA corrpredCRRA corrpredRPMCARA corrpredRPMCRRA, detail
signrank corrpredTWT = corrpredCARA
signrank corrpredTWT = corrpredCRRA
signrank corrpredTWT = corrpredRPMCARA
signrank corrpredTWT = corrpredRPMCRRA

*Produce Figure 2 Right
preserve
gen G =_n
sum  corrpredTWT, detail
gen graphEPV = r(mean)*100 if G==1
ci mean corrpredTWT, level(95)
gen low = r(lb)   if G==1
gen high = r(ub)  if G==1
sum corrpredCRRA, detail
replace graphEPV = r(mean)*100 if G==3
ci mean corrpredCRRA, level(95)
replace low = r(lb)   if G==3
replace high = r(ub)  if G==3
sum corrpredCARA, detail
replace graphEPV = r(mean)*100 if G==5
ci mean corrpredCARA, level(95)
replace low = r(lb)   if G==5
replace high = r(ub)  if G==5
sum corrpredRPMCRRA, detail
replace graphEPV = r(mean)*100 if G==7
ci mean corrpredRPMCRRA, level(95)
replace low = r(lb)   if G==7
replace high = r(ub)  if G==7
sum corrpredRPMCARA, detail
replace graphEPV = r(mean)*100 if G==9
ci mean corrpredRPMCARA, level(95)
replace low = r(lb)   if G==9
replace high = r(ub)  if G==9
keep if G<10
replace low= low*100
replace high =high*100
#delimit ;
twoway 
(bar graphEPV G if G ==1 ,lcolor(black)  fcolor(orange_red))
(bar  graphEPV G if G ==3 ,lcolor(black)  fcolor(gs5) )
(bar  graphEPV G if G ==5 ,lcolor(black)  fcolor(gs8)  )
(bar  graphEPV G if G ==7 ,lcolor(black)  fcolor(gs12)  )
(bar  graphEPV G if G ==9 ,lcolor(black)  fcolor(gs15) )
(rcap high   low G, lcolor(red) lwidth(thick))
,
  graphregion(color(white) fcolor(white) lcolor(white) ) plotregion(fcolor(white) lcolor(white) )
 ytitle("Correctly Predicted Choices (%)" " ", size(large)) 
     ylabel(0(10)80, grid) yline(71.76, lpattern(dash) lcolor(red) lwidth(thick))
 xlabel(1 "TWT" 3"RUM-CRRA" 5"RUM-CARA" 7"RPM-CRRA" 9"RPM-CARA", noticks   )
xtitle(" ") legend(off)  title("KTHDP")
 name(Figure2Right, replace) saving(Figure2Right, replace)
;#delimit cr
graph export Figure2Right.eps, as(eps) preview(on) replace
restore
